package com.xxl.job.admin.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.Map;

/**
 * @author Xumda
 * @version time:2019/12/3 11:04
 * <p>
 * 打印日志拦截器
 */
@SuppressWarnings({"Duplicates", "WeakerAccess"})
@Slf4j
public class LogInterceptor extends HandlerInterceptorAdapter {

    public LogInterceptor() {
        super();
        log.info("添加LogInterceptor拦截器");
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("打印Request日志：");
        if (log.isDebugEnabled()) {
            log.debug("请求url:       {}", request.getRequestURI());
            log.debug("请求method:    {}", request.getMethod());
            for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
                log.debug("parameter: key = {}  value = {}", entry.getKey(), entry.getValue());
            }
            Enumeration<String> headerNames = request.getHeaderNames();
            if (headerNames != null) {
                String name, value;
                while (headerNames.hasMoreElements()) {
                    name = headerNames.nextElement();
                    value = request.getHeader(name);
                    log.debug("header:    name = {}  value = {}", name, value);
                }
            }
        }
        return super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        super.afterCompletion(request, response, handler, ex);
    }

    @Override
    public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        super.afterConcurrentHandlingStarted(request, response, handler);
    }
}
